﻿using BaseData.ApplicationLib.Commands;
using BaseData.ApplicationLib.Queries;
using BaseData.CommonLib.Constants;
using BaseData.CommonLib.Models;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace BaseData.WebApi.Controllers;

[ApiController]
public class SaleOrderController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ISaleOrderQueries _saleOrderQueries;
    
    public SaleOrderController(IMediator mediator,
        ISaleOrderQueries saleOrderQueries)
    {
        _mediator = mediator;
        _saleOrderQueries = saleOrderQueries;
    }

    [HttpGet(ApiRoutes.SaleOrder.QuerySaleOrders)]
    public async Task<IActionResult> Get([FromQuery] SaleOrderSearchRequestDto model)
    {
        if (model.Page > 0 || model.Limit > 0)
        {
            var data = await _saleOrderQueries.QuerySaleOrdersByPage(model);
            return Ok(data);
        }
        else
        {
            var data = await _saleOrderQueries.QuerySaleOrders(model);
            return Ok(data);
        }
    }

    [HttpPost(ApiRoutes.SaleOrder.CreateSaleOrder)]
    public async Task<IActionResult> Post(CreateSaleOrderCommand command)
    {
        var result = await _mediator.Send(command);
        return Ok(result);
    }

    [HttpPut(ApiRoutes.SaleOrder.UpdateSaleOrder)]
    public async Task<IActionResult> Put(int id, UpdateSaleOrderCommand command)
    {
        command.Id = id;
        var result = await _mediator.Send(command);
        return Ok(result);
    }

    [HttpGet(ApiRoutes.SaleOrder.GetSaleOrder)]
    public async Task<IActionResult> Get(int id)
    {
        var result = await _saleOrderQueries.GetSaleOrder(id);
        return Ok(result);
    }

}